#!/bin/bash
########################################################################
# script name:  change_user_login.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2012/06/04    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -o               Old user to change
   -n               New login name for this user

Ex.:
$0 -o b0463152 -n gb008887
STOP
}
#######################################
# Get parameters
#######################################
while getopts o:n: OPTION
do
        case $OPTION in
        o)
                MOVINGID=$(echo $OPTARG | tr '[A-Z]' '[a-z]')
                ;;
        n)
                NEWID=$(echo $OPTARG | tr '[A-Z]' '[a-z]')
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
#######################################
# Variables and environnement
#######################################
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
#######################################
# MAIN
#######################################
if cat /etc/passwd | cut -d: -f1 | grep -wq $MOVINGID
then
	echo Found $MOVINGID
	if who | grep -q $MOVINGID; then echo "$MOVINGID is actually logged in, exiting"; exit 1; fi
	ACTUALDIR=$(cat /etc/passwd | awk -v h=$MOVINGID -F: '{if ($1==h) print $6}')
	TGTDIR=/home/$NEWID
	##
	## Checking if NEWID exist, backing up the home if yes
	##
	if cat /etc/passwd | cut -d: -f1 | grep -wq $NEWID
	then
		##
		## Backuping OLDTGTDIR home directory if it exist
		##
		OLDTGTDIR=$(cat /etc/passwd | awk -v h=$NEWID -F: '{if ($1==h) print $6}')
		if [ -d $OLDTGTDIR ]
		then
			s_OLDTGTDIR=$(echo $OLDTGTDIR | awk -F"/" '{print $NF}').$(date +'%Y-%m-%d-%H-%M')
			ROOTBKPDIR=/home/backup
			BKPDIR=$ROOTBKPDIR/$s_OLDTGTDIR
			echo OLDTGTDIR exist, moving dir $OLDTGTDIR to $BKPDIR
			if [ ! -d $ROOTBKPDIR ]; then mkdir -m 755 $ROOTBKPDIR; fi
			/bin/echo "Moving dir $OLDTGTDIR to $BKPDIR: \c"
			if ! mv $OLDTGTDIR $BKPDIR; then echo "Something went wrong backuping OLDTGTDIR"; exit 1; else echo ok; fi
		fi
		/bin/echo "Removing user $NEWID: \c"
		if ! /usr/sbin/rmuser -p $NEWID; then echo "Something went wrong removing the NEWID user $NEWID"; exit 1; else echo ok; fi
	fi
	##
	## Renaming the actual user to the new name
	##	
	/bin/echo "Renaming user $MOVINGID to $NEWID: \c"
	if ! /usr/sbin/usermod -l $NEWID $MOVINGID; then echo "Something went wrong changing the login of $MOVINGID to $NEWID"; exit 1; else echo ok; fi
	/bin/echo "Changing home dir of $NEWID to $TGTDIR: \c"
	if ! /usr/sbin/usermod -d $TGTDIR $NEWID; then echo "Something went wrong changing the home of $NEWID to $TGTDIR"; exit 1; else echo ok; fi
	##
	## Moving the actual dir ti the TGTDIR (to become the next actual dir
	##	
	/bin/echo "Moving dir $ACTUALDIR to $TGTDIR: \c"
	if ! mv $ACTUALDIR $TGTDIR; then echo "Something went wrong moving ACTUALDIR to $TGTDIR"; exit 1; else echo ok; fi
else
	echo "Username $MOVINGID not found in /etc/passwd"
	exit 1
fi
#######################################
exit 0
